package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService attrValueService;
    @Autowired
    private RoomFacilityService facilityService;
    @Autowired
    private RoomLabelService labelService;
    @Autowired
    private RoomPaymentTypeService paymentTypeService;
    @Autowired
    private RoomLeaseTermService leaseTermService;
    @Autowired
    private LeaseAgreementService leaseAgreementService;

    @Override
    public void customRoomPage(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        roomInfoMapper.customSelectRoomPage(page, queryVo);
    }

    @Override
    public void customSaveOrUpdate(RoomSubmitVo roomSubmitVo) {
//1.判断是保存还是更新
        boolean isUpdate = roomSubmitVo.getId() != null;
        saveOrUpdate(roomSubmitVo);
        if (isUpdate) {
            //删除图片
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, roomSubmitVo.getId());
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);
            //删除属性
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, roomSubmitVo.getId());
            attrValueService.remove(roomAttrValueLambdaQueryWrapper);
            //删除配套
            LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, roomSubmitVo.getId());
            facilityService.remove(roomFacilityLambdaQueryWrapper);
            //删除标签
            LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, roomSubmitVo.getId());
            labelService.remove(roomLabelLambdaQueryWrapper);
            //删除支付方式
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, roomSubmitVo.getId());
            paymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
            //删除租期
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId());
            leaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
        }
        //图片保存
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (CollectionUtils.isNotEmpty(graphVoList)) {
            List<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }
        //属性保存
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (CollectionUtils.isNotEmpty(attrValueIds)) {
            List<RoomAttrValue> roomAttrValueList = new ArrayList<>(attrValueIds.size());
            for (long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().attrValueId(attrValueId)
                        .roomId(roomSubmitVo.getId()).build();
                roomAttrValueList.add(roomAttrValue);
            }
            attrValueService.saveBatch(roomAttrValueList);
        }
        //配套保存
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (CollectionUtils.isNotEmpty(facilityInfoIds)) {
            List<RoomFacility> roomFacilityList = new ArrayList<>(facilityInfoIds.size());
            for (long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = RoomFacility.builder().facilityId(facilityInfoId)
                        .roomId(roomSubmitVo.getId()).build();
                roomFacilityList.add(roomFacility);
            }
            facilityService.saveBatch(roomFacilityList);
        }
        //标签保存
        List<Long> labelIds = roomSubmitVo.getLabelInfoIds();
        if (CollectionUtils.isNotEmpty(labelIds)) {
            List<RoomLabel> roomLabelList = new ArrayList<>(labelIds.size());
            for (long labelId : labelIds) {
                RoomLabel roomLabel = RoomLabel.builder().labelId(labelId)
                        .roomId(roomSubmitVo.getId()).build();
                roomLabelList.add(roomLabel);
            }
            labelService.saveBatch(roomLabelList);
        }
        //支付方式保存
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (CollectionUtils.isNotEmpty(paymentTypeIds)) {
            List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>(paymentTypeIds.size());
            for (long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder().paymentTypeId(paymentTypeId)
                        .roomId(roomSubmitVo.getId()).build();
                roomPaymentTypeList.add(roomPaymentType);
            }
            paymentTypeService.saveBatch(roomPaymentTypeList);
        }
        //租期保存
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (CollectionUtils.isNotEmpty(leaseTermIds)) {
            List<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>(leaseTermIds.size());
            for (long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().leaseTermId(leaseTermId)
                        .roomId(roomSubmitVo.getId()).build();
                roomLeaseTermList.add(roomLeaseTerm);
            }
            leaseTermService.saveBatch(roomLeaseTermList);
        }
    }

    @Override
    public void customRemoveById(Long id) {
        //判断是否存在有效租约
        LambdaQueryWrapper<LeaseAgreement> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomInfoLambdaQueryWrapper.eq(LeaseAgreement::getRoomId, id).in(LeaseAgreement::getStatus,2,5,7);
        long count = leaseAgreementService.count(roomInfoLambdaQueryWrapper);
        if (count > 0) {
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        //删除中间表
        //删除图片
        LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
        graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        graphQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoService.remove(graphQueryWrapper);
        //删除属性
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, id);
        attrValueService.remove(roomAttrValueLambdaQueryWrapper);
        //删除配套
        LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, id);
        facilityService.remove(roomFacilityLambdaQueryWrapper);
        //删除标签
        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, id);
        labelService.remove(roomLabelLambdaQueryWrapper);
        //删除支付方式
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, id);
        paymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
        //删除租期
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
        leaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
        //5.删除数据
        removeById(id);
    }

    @Override
    public RoomDetailVo customGetRoomById(Long id) {
        return roomInfoMapper.customSelectById(id);
    }
}




